package com.woniuxy.happylife.datastore.mqconsumer;

import com.rabbitmq.client.Channel;
import com.woniuxy.happylife.datastore.service.StoreService;
import com.woniuxy.happylife.entity.PhotoData;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * rabbitmq开启监听，处理活动消息
 */
@Component
public class ReleaseConsumer {

    @Resource
    StoreService storeServiceImpl;

    /**
     *
     * @param msg
     * @param bytes
     * @param channel
     * @throws IOException
     */
    @RabbitListener(queues = "queueRelease")
    public void receiveReleaseMessage(String msg, byte[] bytes, Channel channel,@Headers Map<String,Object> headers) throws IOException {
        System.out.println("-----------------");
        System.out.println("消费者处理ReleaseMessage");
        //从message的header里提取出文件参数和deliveryTag
        Object fileName = headers.get("fileName");
        Object originName = headers.get("originName");
        Object contentType = headers.get("contentType");
        Object deliveryTag = headers.get("amqp_deliveryTag");
        long dt = Long.parseLong(deliveryTag.toString());
        //将文件参数和文件byte数组生成mockMultipartFile对象
        MockMultipartFile mockMultipartFile = new MockMultipartFile(fileName.toString(), originName.toString(), contentType.toString(), bytes);
        //调用service存储活动数据
        boolean b = storeServiceImpl.saveRelease(originName.toString(), mockMultipartFile);
        if (b){
            System.out.println(headers.toString());
            System.out.println("存储成功");
            channel.basicAck(dt,false);
        }else {
            System.out.println("存储失败");
            channel.basicNack(dt,false,false);
        }
        System.out.println("-----------------");
    }

    /**
     *
     * @param msg
     * @param bytes
     * @param channel
     * @throws IOException
     */
    @RabbitListener(queues = "queueDynamic")
    public void receiveDynamicMessage(String msg, byte[] bytes, Channel channel, @Headers Map<String,Object> headers) throws IOException {
        System.out.println("-----------------");
        System.out.println("消费者处理DynamicMessage");
        //从message的header里提取出deliveryTag和文件参数
        Object deliveryTag = headers.get("amqp_deliveryTag");
        String dynamicUuidStr = (String) headers.get("dynamicUuidStr");
        String name = (String) headers.get("name");
        String originalFilename = (String) headers.get("originalFilename");
        String contentType = (String) headers.get("contentType");
        long dt = Long.parseLong(deliveryTag.toString());
        System.out.println(dynamicUuidStr);
        //将文件参数和文件byte数组生成MultipartFile对象
        MultipartFile multipartFile =
                new MockMultipartFile(name, originalFilename, contentType, bytes);

        //调用service存储用户动态数据
        String b = storeServiceImpl.saveDynamic(dynamicUuidStr.toString(), multipartFile);
        if(b.equals("动态保存失败")){
            System.out.println(b);
            channel.basicNack(dt,false,false);
        }else {
            System.out.println(headers.toString());
            System.out.println(b);
            channel.basicAck(dt,false);
        }
        System.out.println("-----------------");
    }
}
